<template>
<el-row class="login" type="flex" justify="center" align="middle">
  <el-col :span="8">
  <el-form class="form" label-position="top" :model="loginForm" :rules="rules"     ref="loginForm" label-width="100px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="loginForm.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="loginForm.password" type="password"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">登录</el-button>
      <el-button @click="resetForm()">重置</el-button>
    </el-form-item>
  </el-form>
  </el-col>
</el-row>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) {
          return
        }
        const res = await axios.post('http://localhost:8888/api/private/v1/login', this.loginForm)
        if (res.data.meta.status === 200) {
          this.$message({
            message: res.data.meta.msg,
            type: 'success',
            duration: 800
          })
          localStorage.setItem('token', res.data.data.token)
          this.$router.push('/home')
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: 'error'
          })
        }
        // .then(res => {
        //   if (res.data.meta.status === 200) {
        //     this.$message({
        //       message: res.data.meta.msg,
        //       type: 'success',
        //       duration: 800
        //     })
        //     localStorage.setItem('token', res.data.data.token)
        //     this.$router.push('/home')
        //   } else {
        //     this.$message({
        //       message: res.data.meta.msg,
        //       type: 'error'
        //     })
        //   }
        // })
      })
    },
    resetForm (formName) {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style>
.login {
  height: 100%;
  background-color: #2d434c;
}
.form {
  background-color: #fff;
  padding: 20px;
  border-radius: 15px;
}
</style>
